clear
est clear


cap mat define migrationfigure=J(105,10,.)
		local row=1
		


			
	
	use "$input/full_geoid_pair_panel.dta"
	
	bysort state_fips: egen max=max(storm)
		drop if max!=1
		
	sort geoid_pair move_year
	
		
	replace migration=migration/(tot_pop/100000)
	
			
	preserve
		collapse (sum) migration ,by(to_geoid move_year )

			rename migration in_migration
			rename to_geoid from_geoid
			save "$input/temp1.dta", replace
	restore
	
	
		collapse (sum) migration (mean) gdp,by(from_geoid move_year storm state_fips)
	
		merge 1:1 from_geoid move_year using "$input/temp1.dta"
		
		g net_migration=in_migration-migration
		
		collapse (sum) net_migration storm (mean) gdp, 	by(from_geoid state_fips)
			
		ihstrans(net_migration)	
		
		label var storm "Total storms"
		label var gdp "2019 GDP (MM)"
		
		replace gdp=gdp/1000000
		
		sum gdp,d
			local sd_gdp=trim("`: display %10.3f r(sd)'")
			
		sum storm,d
			local sd_storm=trim("`: display %10.3f r(sd)'")	
		
		reghdfe ihs_net_migration gdp storm, absorb( i.state_fips) vce(cluster state_fips)
			eststo t1c1
			local storm_effect1=trim("`: display %10.3f _b[storm]*`sd_storm''")
			local gdp_effect1=trim("`: display %10.3f _b[gdp]*`sd_gdp''")	
			
		reghdfe ihs_net_migration gdp storm if gdp<1, absorb( i.state_fips) vce(cluster state_fips)
			eststo t1c2
			local storm_effect2=trim("`: display %10.3f _b[storm]*`sd_storm''")
			local gdp_effect2=trim("`: display %10.3f _b[gdp]*`sd_gdp''")	
			
		reghdfe ihs_net_migration gdp storm if gdp>1, absorb( i.state_fips) vce(cluster state_fips)
			eststo t1c3
			local storm_effect3=trim("`: display %10.3f _b[storm]*`sd_storm''")
			local gdp_effect3=trim("`: display %10.3f _b[gdp]*`sd_gdp''")	
		
				
		
			
		
			
	
		
	file open 	t 		using "$tables/table_si10.tex", replace write
	file write t	"\begin{table}[htbp]\footnotesize \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
						"\caption{Impact of GDP and storms on net migration}\label{tab: horse_race}" _n  ///
						"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{10}{c}}" _n "\midrule" _n ///
						"&\multicolumn{1}{c}{Full sample}&\multicolumn{1}{c}{Below median GDP}&\multicolumn{1}{c}{Above median GDP}\\" _n ///
						"\midrule" _n 
	file close 	t
	
				esttab t1c* using "$tables/table_si10.tex", l keep(storm gdp2019) ///
				s(N, l("N") f(%11.0fc) lay(@)) $opts 
				
	file open 	t 		using "$tables/table_si10.tex", append write
	file write t "\\" ///
							"\midrule																							" ///
							"Impact of 1SD change in GDP			&`gdp_effect1'	&`gdp_effect2'		&`gdp_effect3'			\\" ///
							"Impact of 1SD change in storms			&`storm_effect1'	&`storm_effect2'	&`storm_effect3'		\\" ///
							"\midrule																							" ///
							"Fixed Effects:							&				&					&						\\" ///
							"\hspace{3mm}State						&Yes			&Yes				&Yes					\\ " ///
							"\midrule" _n "\end{tabular*}" _n ///
							"\begin{tabular*}{1\textwidth}{p{6.5in}}" _n ///
							"\footnotesize \textsc{Notes:} All columns report the results of a fixed effects specification with the IHS transformation of the number of net-migrants over our sample period from a county as the outcome. Net migration is defined as in-migration minus out-migration so that negative net migration indicates a declining population. Total storms is the sum of the storms the county is impacted by in our sample. \$^{*}\$p=0.1, \$^{**}\$p=0.05,\$^{***}\$p=0.01. Compiled `c(current_date)'." ///
							"\end{tabular*}" _n "\end{table}" _n 
	file close 	t		
	
	
	
	
			
	